<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <!--搜索框-->
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
        </div>
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="账号" size="medium"></el-input>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch"></el-button>
        </div>
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>新增账号</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                style="width: 100%"
        >
            <!--站点-->
            <el-table-column
                    prop="site"
                    label="站点地址"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <!--username-->
            <el-table-column
                    label="用户名"
                    prop="username"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <!--passwords-->
            <el-table-column
                    sortable
                    prop="passwords"
                    label="密码"
                    header-align="center"
                    align="center">
            </el-table-column>
            <!--账号用途-->
            <el-table-column
                    sortable
                    prop="useType"
                    label="账号用途"
                    header-align="center"
                    align="center">
            </el-table-column>
            <!--状态-->
            <el-table-column
                    sortable
                    prop="state"
                    label="状态"
                    header-align="center"
                    align="center">
            </el-table-column>
            <!--备注-->
            <el-table-column
                    sortable
                    prop="remark"
                    label="备注"
                    header-align="center"
                    align="center">
            </el-table-column>
            <!--禁用状态-->
            <el-table-column
                    sortable
                    prop="disabled"
                    label="禁用状态"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <i v-if="scope.row.disabled" class="fa fa-circle text-danger ml5"></i>
                    <i v-if="!scope.row.disabled" class="fa fa-circle text-success ml5"></i>
                </template>
            </el-table-column>
            <!--操作时间-->
            <el-table-column
                    sortable
                    prop="opAt"
                    label="操作时间"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.opAt)}}
                </template>
            </el-table-column>
            <!--操作下拉菜单-->
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                                    :command="{type:'enable',id:scope.row.id,username:scope.row.username,row:scope.row}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item
                                    :command="{type:'disable',id:scope.row.id,username:scope.row.username,row:scope.row}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'loginHg',id:scope.row.id}">
                                登录
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'edit',id:scope.row.id,oldName:scope.row.username}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.id,username:scope.row.username}"
                                              v-if="scope.row.username!='superadmin'">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页容器-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <!--新增/修改按钮弹出的dialog-->
    <el-dialog
            :title="titlePopu==0?'新增账号':'修改账号'"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-form :model="formData" ref="editForm" :rules="formRules" size="small" label-width="80px">
            <!--            新增/修改站点input-->
            <el-form-item prop="site" :label="titlePopu==0?'输入站点地址':'修改站点地址'">
                <el-input maxlength="100" :placeholder="titlePopu==0?'输入站点地址':'修改站点地址'"
                          v-model="formData.site"
                          auto-complete="off"
                          tabindex="1"
                          type="text"
                >
                </el-input>
            </el-form-item>
            <!--            新增/修改username-->
            <el-form-item prop="username" :label="titlePopu==0?'输入账号用户名':'修改账号用户名'">
                <el-input maxlength="100"
                          :placeholder="titlePopu==0?'输入账号用户名':'修改账号用户名'"
                          v-model="formData.username"
                          auto-complete="off"
                          tabindex="1"
                          type="text">
                </el-input>
            </el-form-item>
            <!--            新增/修改passwords-->
            <el-form-item prop="passwords" :label="titlePopu==0?'输入账号密码':'修改账号密码'">
                <el-input maxlength="100"
                          :placeholder="titlePopu==0?'输入账号密码':'修改账号密码'"
                          v-model="formData.passwords"
                          auto-complete="off"
                          tabindex="1"
                          type="text">
                </el-input>
            </el-form-item>
            <!--            新增/修改useType-->
            <el-form-item prop="useType" :label="titlePopu==0?'输入账号用途':'修改账号用途'">
                <el-input maxlength="100"
                          :placeholder="titlePopu==0?'输入账号用途':'修改账号用途'"
                          v-model="formData.useType"
                          auto-complete="off"
                          tabindex="1"
                          type="text">
                </el-input>
            </el-form-item>
        </el-form>
        <!--        确认/取消 提交修改按钮-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEdit">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            var self = this;
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        var threeMonths = moment().add(-3, 'months').valueOf();
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
                            || time.getTime() < threeMonths;
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                searchDate: [],
                editDialogVisible: false,
                titlePopu: 0,
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    searchKeyword: "",
                    searchDate: ""
                },
                formData: {
                    oldName: "",
                    site: "",
                    realSite: "",
                    username: "",
                    passwords: "",
                    useType: "",
                    disabled: false
                },
                formRules: {
                    // 新增账号
                    site: [
                        {required: true, message: '请输入站点地址', trigger: 'blur'}
                    ],
                    username: [
                        {required: true, message: '请输入账号用户名', trigger: 'blur'}
                    ],
                    passwords: [
                        {required: true, message: '请输入账号密码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/crown/config/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            openAdd: function () {// 新建/修改按钮逻辑
                var self = this;
                self.titlePopu = 0;
                this.editDialogVisible = true;
                //打开新增窗口,表单先清空
                this.formData = {};
                if (this.$refs["editForm"]) {
                    this.$refs["editForm"].resetFields();
                }
            },
            doEdit: function () { // 提交新增/修改
                var self = this;
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {
                        //titlePopu=0是新增,1是修改
                        if (self.titlePopu == 0) {
                            $.post(base + "/platform/crown/config/add", self.formData, function (data) {
                                if (data.code == 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.doSearch();
                                    self.editDialogVisible = false;
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        } else {
                            $.post(base + "/platform/crown/config/editDo", self.formData, function (data) {
                                if (data.code == 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.doSearch();
                                    self.editDialogVisible = false;
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                // 启用，禁用按钮
                if ("enable" == command.type || "disable" == command.type) {
                    $.post(base + "/platform/crown/config/" + command.type + "/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.$message({
                                message: data.msg,
                                type: 'success'
                            });
                            if ("disable" == command.type) {
                                command.row.disabled = true;
                            }
                            if ("enable" == command.type) {
                                command.row.disabled = false;
                            }
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                // 登录HG
                if ("loginHg" == command.type) {
                    $.post(base + "/platform/crown/config/edit/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.formData = data.data;
                            self.formData.realSite = data.data.site;
                            // 请求登录接口
                            $.post(base + "/platform/crown/config/loginHg", self.formData, function (data) {
                                if (data.code == 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.doSearch();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                // 修改按钮
                if ("edit" == command.type) {
                    $.post(base + "/platform/crown/config/edit/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            if (self.$refs["editForm"]) {// 清空校验提示
                                self.$refs["editForm"].resetFields();
                            }
                            self.formData = data.data;// 加载后台表单数据
                            self.formData.oldName = data.data.username;
                            self.editDialogVisible = true;// 打开编辑窗口
                            self.titlePopu = 1;
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                // 删除按钮
                if ("delete" == command.type) {
                    self.$confirm('此操作将删除 ' + command.username, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" == a) {//确认后再执行
                                $.post(base + "/platform/crown/config/delete/" + command.id, {}, function (data) {
                                    if (data.code == 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            }


        },
        created: function () {
            this.pageData();
        }
    });
</script>

<!--#
}
#-->